//
//  ChooseDateView.m
//  BrushFace
//
//  Created by timer_q on 15/12/23.
//  Copyright © 2015年 Zzz. All rights reserved.
//

#import "ChooseDateView.h"
#import "IWillHelper.h"

@implementation ChooseDateView
{
    CGRect _frame;
    UIPickerView *_mPickerV;
    UIPickerView *_dPickerV;
    NSArray *_mArray;
    NSArray *_dArray;
    NSArray *_daysArr;
    NSArray *_pArray;
    NSString *_retStr;
    NSString *_proid;
    NSString *_days;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        _frame = frame;
        [self UIConfig];
        [self dataInit];
    }
    return self;
}
#pragma mark - customMethods
- (void)dataInit{
    _mArray = [[NSArray alloc]initWithObjects:@"一个月内",@"1个月",@"2个月",@"3个月",@"4个月",@"5个月",@"6个月",@"7个月",@"8个月",@"9个月",@"10个月",@"11个月",@"12个月",nil];
    _pArray = [[NSArray alloc]initWithObjects:@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12",nil];
    _dArray = [[NSArray alloc]initWithObjects:@"5天",@"10天",@"15天",@"20天",nil];
    _daysArr = [[NSArray alloc]initWithObjects:@"5",@"10",@"15",@"20",nil];
    _proid = @"0";
    _days = @"5";
    _retStr = [_mArray objectAtIndex:0];
    
}
- (void)UIConfig{
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _frame.size.width, _frame.size.height)];
    backView.backgroundColor = [UIColor blackColor];
    backView.alpha = 0.45;
    [self addSubview:backView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hiddenChooseView)];
    [backView addGestureRecognizer:tap];
    
    _mPickerV = [[UIPickerView alloc] initWithFrame:CGRectMake(0,_frame.size.height- _frame.size.width/2.5, _frame.size.width, _frame.size.width/2.5)];
    _mPickerV.backgroundColor = [UIColor whiteColor];
    // 显示选中框
    _mPickerV.showsSelectionIndicator=YES;
    _mPickerV.dataSource = self;
    _mPickerV.delegate = self;
    [self addSubview:_mPickerV];
    
    _dPickerV = [[UIPickerView alloc] initWithFrame:CGRectMake(_frame.size.width/2.0+_frame.size.width/2.0*0.5,_frame.size.height- _frame.size.width/2.5, _frame.size.width/2.0*0.5, _frame.size.width/2.5)];
    _dPickerV.backgroundColor = [UIColor clearColor];
    // 显示选中框
    _dPickerV.showsSelectionIndicator=YES;
    _dPickerV.dataSource = self;
    _dPickerV.delegate = self;
    [self addSubview:_dPickerV];
    
    UIView *labelBackView = [[UIView alloc] initWithFrame:CGRectMake(0, _mPickerV.frame.origin.y-50, _frame.size.width, 50)];
    labelBackView.backgroundColor = [UIColor whiteColor];
    [self addSubview:labelBackView];
    
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    cancelBtn.frame = CGRectMake(0, 0, _frame.size.width/4.0, 50);
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(cancelButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [labelBackView addSubview:cancelBtn];
    
    UIButton *doneBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    doneBtn.frame = CGRectMake(_frame.size.width*0.75, 0, _frame.size.width/4.0, 50);
    [doneBtn setTitle:@"确定" forState:UIControlStateNormal];
    [doneBtn setTitleColor:ColorDefaultBlue forState:UIControlStateNormal];
    [doneBtn addTarget:self action:@selector(doneButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [labelBackView addSubview:doneBtn];
    
    UILabel *label = [[UILabel alloc] init];
    label.frame = CGRectMake(_frame.size.width*0.25, 0, _frame.size.width*0.5, 50);
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor redColor];
    label.text = @"选择借款期限";
    [labelBackView addSubview:label];
}

#pragma mark - pickV delegate
// pickerView 列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}
// pickerView 每列个数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if ([pickerView isEqual:_mPickerV]) {
        return [_mArray count];
    }else{
        return [_dArray count];
    }
}
// 每列宽度
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    
    if ([pickerView isEqual:_mPickerV]) {
        return _frame.size.width;
    }
    return _dPickerV.frame.size.width;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 40;
}
// 返回选中的行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if ([_mPickerV isEqual:pickerView]) {
        if (row == 0) {
            _dPickerV.hidden = NO;
        }else{
            for (UIView *view in self.subviews) {
                if ([view isEqual:_dPickerV]) {
                    [view removeFromSuperview];
                }
            }
            _dPickerV = [[UIPickerView alloc] initWithFrame:CGRectMake(_frame.size.width/2.0+_frame.size.width/2.0*0.5,_frame.size.height- _frame.size.width/2.5, _frame.size.width/2.0*0.5, _frame.size.width/2.5)];
            _dPickerV.backgroundColor = [UIColor clearColor];
            // 显示选中框
            _dPickerV.showsSelectionIndicator=YES;
            _dPickerV.dataSource = self;
            _dPickerV.delegate = self;
            [self addSubview:_dPickerV];
            _days = @"5";

            _dPickerV.hidden = YES;
            _retStr = [_mArray objectAtIndex:row];
            _proid = [_pArray objectAtIndex:row];
        }
    } else {
        _retStr = [_dArray objectAtIndex:row];
        _days = [_daysArr objectAtIndex:row];
    }
    
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0,
    [pickerView rowSizeForComponent:component].width, [pickerView rowSizeForComponent:component].height)];
    if ([_mPickerV isEqual:pickerView]) {
        label.text =[_mArray objectAtIndex:row];
        label.textAlignment = NSTextAlignmentCenter;
    } else {
        label.text =[_dArray objectAtIndex:row];
        label.textAlignment = NSTextAlignmentLeft;
    }
    
    return label;
}

#pragma mark - action 
- (void)chooseViewWillShow{
    for (UIView *view in self.subviews) {
        [view removeFromSuperview];
    }
    [self UIConfig];
    
    _proid = @"0";
    _days = @"5";
    _retStr = @"5天";
}
- (void)hiddenChooseView{
    self.hidden = YES;
}
- (void)cancelButtonClick{
    self.hidden = YES;
}
- (void)doneButtonClick{
    self.hidden = YES;
    [self.delegate chooseDate:_retStr andProid:_proid andDays:_days];
}
@end
